<?php
namespace jy_quanhuo;
defined('BY_JYA') or exit('error');
/*
	订单
*/
class order extends \plugin{
	//订单创建时，执行此方法，执行相关操作
	public $sendtypes=array(
		'1'=>'快递配送',
		'2'=>'自提/核销',
		'3'=>'虚拟发货',
	);
	public function create($params,$uid='',$url='',$notpl=false){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		$ordersn=$params['ordersn']?:'SN'.get_ordersn('jy_merch_game_order','ordersn');
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$ordersn,
			'mid'=>$params['mid']?:$uid,
			'merchid'=>$params['merchid'],
			'pid'=>$params['pid'],
			'gamename'=>$params['gamename'],
			'goods'=>ijson_encode($params['goods']),
			'canyuform'=>ijson_encode($params['canyuform']),
			'diyform'=>ijson_encode($params['diyform']),
			'sendtype'=>$params['sendtype'],
			'addressid'=>$params['addressid'],
			'address'=>ijson_encode($params['address']),
			'realname'=>$params['realname'],
			'mobile'=>$params['mobile'],
			'fee'=>$params['fee'],
			'remark'=>trim($params['remark']),
			'status'=>$params['status'],
			'tihuo_id'=>$params['tihuo_id'],
			'is_xuni'=>$params['is_xuni'],
			'createtime'=>TIMESTAMP,
		);
		/*
			核销
		*/
		$num=0;
		$verifynum=0;
		foreach($params['goods'] as $key=>&$gv){
			$this->check_buy($params['pid'],$key,$gv['cartnum']);	
		}
		pdo_insert('jy_merch_game_order',$order);
		$order_id=$this->pdo_insertid();
		$this->order['id']=$order_id;
		$this->order['ordersn']=$order['ordersn'];
		$member=pdo_get('core_members',array('id'=>$this->order['mid']),array('mobile','nickname','realname'));
		//完善会员信息
		if((!$member['mobile'] && $this->order['mobile']) || (!$member['realname'] && $this->order['realname'])){
			model('core/member')->member_update($order['mid'],array('mobile'=>$member['mobile']?:$this->order['mobile'],'realname'=>$member['realname']?:$this->order['realname']));
		}		
		//整理订单商品
		foreach($params['goods'] as $k=>$item){
			if($item['cartnum']>0){
				$new_order_goods=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$order['mid'],
					'merchid'=>$order['merchid'],
					'pid'=>$order['pid'],
					'orderid'=>$order_id,
					'goodskey'=>$k,
					'price'=>$item['price'],
					'total'=>$item['cartnum'],
					'totalprice'=>$item['cartnum']*$item['price'],
					'gamename'=>$params['gamename'],
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_merch_game_order_goods',$new_order_goods);
			}
		}
		$this->model('member')->up_fromuser($order['pid'],'','',$order['merchid'],1);
		return $order_id;
	}
	public function get_order($order_id,$merchid=0){
		if($this->order && $this->order['id']==$order_id){
			return $this->order;
		}
		$this->order=pdo_get('jy_merch_game_order',array('id'=>$order_id,'uniacid'=>$_SESSION['uniacid']));
		$this->order['goods']=json_decode($this->order['goods'],true);
		return $this->order;
	}
	public function check_buy($pid,$key=0,$cartnum=1){
		return false;
	}
	public function mianup($pid,$fromuser=0){
		if(!$fromuser){
			return false;
		}
		$isup=pdo_get('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'orderid >'=>0,'pid'=>$pid,'mid'=>$fromuser,'fromuser'=>-1));
		if($isup['id']){
				$update=array(
					'isteamnum +='=>1,
				);
				if(!$isup['teamnum']){
					$isup['teamnum']=2;
					$update['teamnum']=2;
				}
				if($isup['isteamnum']+1>=$isup['teamnum']){
					$update['status']=1;
				}
				pdo_update('jy_merch_game_fromuser',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$isup['id']));
				if($update['status'] && $isup['orderid']){
					$params=array(
						'status'=>1,
						'paytype'=>'miandan',
						'transid'=>$isup['id'],
					);
					$this->model('order')->order_pay($isup['orderid'],$params);
				}
		}
		return false;
	}
	//支付
	public function order_pay($order_id,$params,$url='',$notpl=false){
		$this->get_order($order_id);
		if(!$this->order || $this->order['ispay']){
			exi('订单不存在或者已支付！','error');
		}
		$update=array(
			'status'=>$params['status']?:1,
			'paytype'=>$params['paytype'],
			'transid'=>$params['transid'],
		);
		if($params['paytype']=='hdfk'){
			$update['ispay']=0;
		}else{
			$update['ispay']=1;
			$update['paytime']=TIMESTAMP;
		}
		foreach($update as $key=>$value){
			$this->order[$key]=$value;
		}
		pdo_update('jy_merch_game_order',$update,array('id'=>$order_id));
		pdo_update('jy_merch_game_order_goods',array('status'=>1,'ispay'=>$update['ispay'],'paytime'=>TIMESTAMP),array('orderid'=>$order_id));
		$this->model('member')->up_fromuser($this->order['pid'],'','',$this->order['merchid'],2);
		$this->model('commission')->create($this->order);
		$setting=$this->plugin_setting('basic');
		if($setting['mian_type']){
			$fromuser=pdo_getcolumn('jy_merch_game_fromuser',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$this->order['pid'],'mid'=>$this->order['mid']),'fromuser');
			$this->model('order')->mianup($this->order['pid'],$fromuser);
		}
		return true;
	}
}